/*
 * @lc app=leetcode.cn id=232 lang=typescript
 *
 * [232] 用栈实现队列
 */

// @lc code=start
class MyQueue {
    private innerStack: number[] = [];
    private cacheStack: number[] = [];
    constructor() {}

    push(x: number): void {
        this.putBackToStack();
        this.innerStack.push(x);
    }

    private putBackToStack() {
        while (this.cacheStack.length) {
            this.innerStack.push(this.cacheStack.pop()!);
        }
    }
    private putToCacheStack() {
        while (this.innerStack.length) {
            this.cacheStack.push(this.innerStack.pop()!);
        }
    }
    pop(): number {
        this.putToCacheStack();
        return this.cacheStack.pop()!;
    }

    peek(): number {
        this.putToCacheStack();
        // 这个是查看栈顶，所以可以
        return this.cacheStack[this.cacheStack.length - 1];
    }

    empty(): boolean {
        return !(this.innerStack.length + this.cacheStack.length);
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * var obj = new MyQueue()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.empty()
 */
// @lc code=end
